package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsQrCodeInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsQrCodeInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.QrCodeInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.MchntQrCodeModifyRequest;
import cc.rengu.igas.bsps.facade.response.MchntQrCodeModifyResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * @author zhangxuran
 * @version 1.0
 * 二维码信息修改
 */
public class MchntQrCodeModifyService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntQrCodeModifyRequest mchntQrCodeModifyRequest = new MchntQrCodeModifyRequest();
        ConvertUtil.convertOutput(mchntQrCodeModifyRequest);
        return mchntQrCodeModifyRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntQrCodeModifyRequest mchntQrCodeModifyRequest = (MchntQrCodeModifyRequest) request;
        MchntQrCodeModifyResponse mchntQrCodeModifyResponse = new MchntQrCodeModifyResponse();
        BizResponse<MchntQrCodeModifyResponse> bizResponse = new BizResponse<>();
        /*获取法人行号标识、用户标识、商户号、二维码URL、二维码编码*/
        String instId = mchntQrCodeModifyRequest.getHeader().getInstId();
        String userId = mchntQrCodeModifyRequest.getUserId();
        String mchntNo = mchntQrCodeModifyRequest.getMchntNo();
        String qrUrl = mchntQrCodeModifyRequest.getQrUrl();
        String qrCode = mchntQrCodeModifyRequest.getQrCode();
        String qrCodeName = mchntQrCodeModifyRequest.getQrCodeName();

        rglog.info("法人行标识:" + instId + "用户标识:" + userId + "商户号:" + mchntNo + "二维码URL:" + qrUrl + "二维码编码:" + qrCode);
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(mchntNo) ||
                (StringUtil.isEmptyOrNull(qrUrl) && StringUtil.isEmptyOrNull(qrCode))) {
            rglog.error("必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        qrUrl = qrUrl.replaceAll("pay/PYCodePay.do", "epay-h5/COPS/qr-code-payment");
        /*查询二维码信息，判断二维码是否允许相应操作*/
        BspsQrCodeInfoMapper qrCodeMapper = new BspsQrCodeInfoMapperImpl();
        QrCodeInfo qrCodeInfo;
        qrCodeInfo = queryQrcoeInfo(instId, qrUrl, qrCode);
        if (null == qrCodeInfo) {
            rglog.error("二维码不存在");
        }
        //判断二维码状态
        if (!"00".equals(qrCodeInfo.getQrStatus())) {
            rglog.error("二维码状态异常" + qrCodeInfo.getQrStatus());
            throw new BizException(RespCodeEnum.INVALID_QRCODE_ERROR.getRespCode(), RespCodeEnum.INVALID_QRCODE_ERROR.getRespDesc());
        }

        if (!StringUtil.isEmptyOrNull(qrCodeInfo.getMchntNo()) && !mchntNo.equals(qrCodeInfo.getMchntNo())) {
            rglog.error("二维码属于商户:" + qrCodeInfo.getMchntNo());
            throw new BizException(RespCodeEnum.ILLEGAL_OPERATION.getRespCode(), RespCodeEnum.ILLEGAL_OPERATION.getRespDesc());
        }
        qrCodeInfo.setLastOprId(userId);
        qrCodeInfo.setEffectiveTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        qrCodeInfo.setLastUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        qrCodeInfo.setQrCodeName(qrCodeName);
        int iReturnCode = qrCodeMapper.updateBspsQrCodeInfoById(qrCodeInfo);
        if (0 != iReturnCode) {
            rglog.error("更新二维码信息失败!");
            throw new BizException(RespCodeEnum.UPDATE_QRCODE_INFO_ERROR.getRespCode(), RespCodeEnum.UPDATE_QRCODE_INFO_ERROR.getRespDesc());
        }

        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntQrCodeModifyResponse);
        return bizResponse;
    }

    /**
     * 根据二维码url或者qrCode查询二维码信息
     *
     * @param instId 法人行号
     * @param qrUrl  二维码URL
     * @param qrCode 二维码编码
     *               return qrcodeInfo
     */
    private QrCodeInfo queryQrcoeInfo(String instId, String qrUrl, String qrCode) throws Exception {
        BspsQrCodeInfoMapper qrCodeInfoMapper = new BspsQrCodeInfoMapperImpl();
        QrCodeInfo queryQrcodeInfo;
        if (!StringUtil.isEmptyOrNull(qrUrl)) {
            rglog.info("根据qrUrl查询二维码信息：" + qrUrl);
            queryQrcodeInfo = qrCodeInfoMapper.selectBspsQrCodeInfoByPrimaryKey(instId, qrUrl);
        } else {
            rglog.info("根据qrCode查询二维码信息：" + qrCode);
            queryQrcodeInfo = qrCodeInfoMapper.selectBspsQrCodeInfoByQrCode(instId, qrCode);
        }
        return queryQrcodeInfo;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntQrCodeModifyResponse mchntQrCodeModifyResponse = (MchntQrCodeModifyResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntQrCodeModifyResponse);
    }
}
